package com.hp.activiti.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.hp.activiti.annotation.ApiOpt;
import com.hp.activiti.bean.AuthUser;
import com.hp.activiti.bean.OptTypeEnum;
import com.hp.activiti.bean.Result;
import com.hp.activiti.model.UserInfo;
import com.hp.activiti.service.UserService;

/**
 * @Description:
 * @author hp
 * @date 2018年5月20日 上午10:26:25
 */
@RestController
@RequestMapping(value="/sys")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@PostMapping("/userinfo")
    public UserInfo userinfo() {
		AuthUser userInfo = (AuthUser) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
		return userInfo.getUser();
    }
	
	@RequestMapping(value="/queryUser/{account}")
	@ApiOpt(optType=OptTypeEnum.QUERY)
	public UserInfo queryUser(@PathVariable String account){
		return userService.queryUser(account);
	}
	
	
	/**
	 * 查询所有用户 根据管理部门
	 * @param account
	 * @return
	 */
	@RequestMapping(value="/findByDepartment/{depCode}")
	@ApiOpt(optType=OptTypeEnum.QUERY)
	public List<UserInfo> findByDepartment(@PathVariable String depCode){
		return userService.findByDepartment(depCode);
	}
	
	/**
	 * 根据userId查询用户
	 * @param account
	 * @return
	 */
	@RequestMapping(value="/findById/{id}")
	public UserInfo findById(@PathVariable Long id){
		return userService.findById(id);
	}
	
	@RequestMapping(value="/saveUser/{userId}/{userName}/{roleStr}",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Result saveUser(@PathVariable Long userId, @PathVariable String userName, @PathVariable String roleStr){
		return userService.saveUser(userId,userName,roleStr);
	}
	
	@RequestMapping(value="/saveUser",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Result saveUser(@RequestBody UserInfo user){
		return userService.saveUser(user);
	}
	
}
